<?php
/*
* Class teia\webdev\php\adt\Item
* Core class for items
*
* @author Bernardo Quibiana
* @date created: 20111002
*
* @date modified: 20111002
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\adt;

class Item implements IYamlItem {
	protected $id;
	protected $name;
	protected $description;
	protected $comment;
	protected $components;
	
	public function __construct($id, $name, $description = NULL, $comment = NULL, $components = NULL) {
		$this->id = $id;
		$this->name = $name;
		if ($description) {
			$this->description = $description;
		}
		if ($comment) {
			$this->comment = $comment;
		}
		if ($components && is_array($components)) {
			$this->components = $components;
		}
	}
	
	public function __get($property) {
		return  $this->$property;
	}
	
	public function __set($property, $value) {
		return $this->$property = $value;
	}
	
	public function initWithYamlArray($array) {
		$this->id = $array["id"];
		$this->name = $array["name"];
		$this->description = $array["description"];
		$this->comment = $array["comment"];
	}
	
	public function initWithYamlObject($yaml) {
		$array = yaml_parse($yaml);
		$this->initWithYamlArray($array);
	}
	
	public function initWithYamlStream($url) {
		$array = yaml_parse_file($path);
		$this->initWithYamlArray($array);
	}
	
	public function initWithYamlFile($path) {
		$array = yaml_parse_file($path);
		$this->initWithYamlArray($array);
	}
	
	public function generateYamlArray() {
		return array ("id"=> $this->id, "name"=> $this->name, "description"=> $this->description, "comment"=> $this->comment, );
	}
	
	public function generateYamlObject() {
		$array = $this->generateYamlArray();
		return yaml_emit($array);
	}
	
	public function saveYamlObjectToFile($path) {
		$yamlObj = $this->generateYamlObject();
		return yaml_emit_file($path, $yamlObj, YAML_UTF8_ENCODING);
	}
}

?>